package kaumono.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import kaumono.service.RootService;
import net.arnx.jsonic.JSON;
import susano.common.util.BeanMap;
import susano.presen.controller.Action;
import susano.presen.controller.Navigation;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

/**
 * save action.
 *
 * @author tanaka.akira.2006
 * @version $Id: SaveAction.java 687 2009-11-13 07:55:17Z tanaka.akira.2006 $
 *
 */
public class SaveAction extends Action {

	protected static final Logger log = Logger.getLogger(SaveAction.class
			.getName());

	private RootService rootService = new RootService();

	@Override
	protected Navigation init() {
		return forward("index.jsp");
	}

	@Override
	protected Navigation execute() {

		String jsonstr = asString("data");
		BeanMap bm = jsonToBeanMap(jsonstr);

		String keystr = rootService.createKeyAsString();

		// TODO: Rootエンティティを取得
		BeanMap root = rootService.get(keystr);
		// TODO: Rootエンティティのプロパティを上書き
		root.put("pubFlg", bm.get("pubFlg"));

		// TODO: Itemエンティティ一覧を取得
		// TODO: 既存のItemsを全削除

		// TODO: 新しいItemsを保存

//		List<String> items = (ArrayList<String>) map.get("items");
		return forward("index.jsp");
	}

	@Override
	protected Navigation validate() {
		return null;
	}

	private BeanMap jsonToBeanMap(final String jsonstr) {
		BeanMap map = (BeanMap) JSON.decode(jsonstr);
		return map;
	}

}
